# Copyright 2009 Larry Resnik, Lasfraka-Elixir

#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.


from __future__ import division

import pyglet

class Camera(object):
    """Viewing aid for seeing only the tiles within sight. 32x32 only.

    This class doesn't do the drawing for the game.
    """

    def __init__(self, mapWidth, mapHeight):
        self.mapWidth = mapWidth
        self.mapHeight = mapHeight
        self.visibleX = 10
        self.visibleY = 8
        self.halfVisibleX = int(self.visibleX / 2)
        self.halfVisibleY = int(self.visibleY / 2)
        self.centerX = self.halfVisibleX * 32
        self.centerY = self.halfVisibleY * 32
        crosshairImage = pyglet.resource.image('camera_center.png')
        crosshairImage.anchor_x = int(crosshairImage.width / 2)
        crosshairImage.anchor_y = int(crosshairImage.height / 2)
        self.crosshair = pyglet.sprite.Sprite(crosshairImage, self.centerX,
            self.centerY)

    def determine_visible_x(self, eyeX):
        """Determine the slice of the 2D tile array to display.

        eyeX is a pixel-based coordinate.
        Returns how far the eye is from the background tiles' origins.
        """

        positionX = int(eyeX / 32)
        beginX = positionX - self.halfVisibleX
        endX = positionX + self.halfVisibleX + 1
        if beginX < 0:
            # eye.x is to the left of level's left end + halfVisibleX.
            return eyeX % 32 + beginX * 32
        elif endX > self.mapWidth:
            # eye.x is to the right of level's right end - halfVisibleX - 1.
            return eyeX % 32 - (self.mapWidth - endX + 1) * 32
        # eye.x is between the halfVisibleX extremes.
        return 0

    def determine_visible_y(self, eyeY):
        """Determine the slice of the 2D tile array to display.

        eyeY is a pixel-based coordinate.
        Returns how far the eye is from the background tiles' origins.
        """

        positionY = int(eyeY / 32)
        beginY = positionY - 3
        endY = positionY + self.halfVisibleY + 1
        if beginY < 0:
            # eye.y is below the level's bottom + halfVisibleY.
            # Note: +4 draws the furthest tiles.
            return eyeY % 32 + beginY * 32 - 32
        elif endY >= self.mapHeight:
            # eye.y is above the level's top - halfVisibleY.
            # Note: -12 draws the furthest tiles.
            return eyeY % 32 - (self.mapHeight - endY) * 32 - 32
        # eye.y is between the halfVisibleY extremes.
        return -32

    def draw(self):
        if self.crosshair:
            self.crosshair.draw()
